package com.yulan.Array;

import org.junit.Test;

import java.util.Arrays;

public class LeetCode_256_SmallerThreeSum {

    public int threeSumSmaller(int[] nums, int target) {
        int len = nums.length;
        int count = 0;
        if(len < 3)
            return 0;
        Arrays.sort(nums);
        for(int i=0; i<len; i++){
            int l = i + 1;
            int r = len - 1;
            while(l < r){
                int sum = nums[i] + nums[l] + nums[r];
                if(sum < target){
                    count += r - l;
                    r--;
                }else{
                    l++;
                }
            }
        }
        return count;
    }

    @Test
    public void test(){
        int[] arr = {-2,0,1,3};
        System.out.println(threeSumSmaller(arr,2));
    }
}
